// pages/work/work.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    nickName: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (!wx.getStorageSync('openid_key')) {
      wx.navigateTo({
        url: '../login/index/index',
      })
    }
    this.setData({
      homeworkid: options.id
    })
    wx.getUserInfo({
      lang: "zh_CN",
      success: res => {
        this.setData({
          nickName: res.userInfo.nickName
        })
      }
    })
  },

  ChooseImage() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: res => {
        const tempFilePaths = res.tempFilePaths
        const _data = this
        wx.uploadFile({
          url: 'http://127.0.0.1:8110/admin/edu/file/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          success: res => {
            // console.log(res.data);
            const data = JSON.parse(res.data)
            // console.log(data);
            // console.log(data);
            // const urlData = JSON.stringify(data)
            // console.log(urlData);
            console.log(data.data.url);
            this.setData({
              imgList: this.data.imgList.concat(data.data.url)
            })
            // if (this.data.imgList.length != 0) {
            //   this.setData({
            //     imgList: this.data.imgList.concat(data.data.url)
            //   })
            //   // console.log(res.tempFilePaths);
            // } else {
            //   console.log(this.data.imgList)
            //   this.setData({
            //     imgList: data.data.url
            //   })
            // }
          }
        })
      }
    })
  },


  //   wx.chooseImage({
  //     count: 4, //默认9
  //     sizeType: ['original', 'compressed'],
  //     sourceType: ['album'],
  //     success: (res) => {
  //       if (this.data.imgList.length != 0) {
  //         this.setData({
  //           imgList: this.data.imgList.concat(res.tempFilePaths)

  //         })
  //         // console.log(res.tempFilePaths);
  //       } else {
  //         this.setData({
  //           imgList: res.tempFilePaths
  //         })
  //       }
  //     }
  //   });
  // },

  // /admin/edu/file/upload file
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除',
      content: '确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  formSubmit(e) {
    var that = this
    console.log(that.data.nickName);
    const videoid = wx.getStorageSync('videoid_key')
    const openid = wx.getStorageSync('openid_key')
    const courseid = wx.getStorageSync('courseid_key')
    const chapterid = wx.getStorageSync('chapterid_key')

    console.log(this.data.imgList);
    wx.request({
      url: 'http://127.0.0.1:8110/admin/edu/homework/submit ',
      data: {
        'courseId': courseid,
        "chapterId": chapterid,
        'openid': openid,
        'nickName': that.data.nickName, 
        'videoId': videoid,
        'value': that.data.imgList
      },
      method: 'POST',
      "Content-Type": "application/x-www-form-urlencoded",
      type: "json",
      success: function (res) {
        console.log(res);
        wx.showModal({
          title: '提示',
          content: res.data.message,
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })

        // console.log(res.data.data.chapterVoList);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})